package com.nondo.rdp.finance.endpoint.api.app;

import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.setting.advert.Advert;
import com.nondo.rdp.finance.domain.setting.home.*;
import com.nondo.rdp.finance.logic.SettingModule.SettingModuleLogic;
import com.nondo.rdp.finance.model.setting.home.*;
import com.nondo.rdp.finance.service.setting.advert.AdvertService;
import com.nondo.rdp.finance.service.setting.home.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController("apiAppHome")
@RequestMapping("/api/app/home")
public class HomeEndpoint {
    @Autowired
    SettingModuleService settingModuleService;

    @Autowired
    SettingModuleSlideService settingModuleSlideService;

    @Autowired
    SettingModuleSecurityOpenService settingModuleSecurityOpenService;

    @Autowired
    SettingModuleNoticeService settingModuleNoticeService;

    @Autowired
    SettingModuleIconService settingModuleIconService;

    @Autowired
    SettingModuleEducationService settingModuleEducationService;

    @Autowired
    SettingModuleLogic settingModuleLogic;

    @Autowired
    SettingModuleCustomerService settingModuleCustomerService;

    @Autowired
    SettingModuleProductService settingModuleProductService;

    @Autowired
    AdvertService advertService;

    @RequestMapping("")
    public View index(HttpServletRequest request, HttpServletResponse response) {
        View view = new View(20000, "获取数据成功");

        //一、获取所有模块列表
        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModule> settingModuleList = this.settingModuleService.findAllByStatus(sort);
        //二、循环处理所有模块
        List newList = new ArrayList();
        for (SettingModule item : settingModuleList) {
            //1、复制到新的bean用于输出组装
            SettingModuleBean settingModuleBean = new SettingModuleBean();
            BeanUtils.copy(item, settingModuleBean);
            //2、根据不同类型处理模块
            switch (item.getModuleType()) {
                case SettingModule.TYPE_SLIDE:
                    settingModuleBean.setContent(this.getSliceList());
                    break;
                case SettingModule.TYPE_SECURITY_OPEN:
                    settingModuleBean.setContent(this.getSecurityOpenList());
                    break;
                case SettingModule.TYPE_NOTICE:
                    settingModuleBean.setContent(this.getNoticeList());
                    break;
                case SettingModule.TYPE_ICON:
                    settingModuleBean.setContent(this.getIconList());
                    break;
                case SettingModule.TYPE_EDUCATION:
                    settingModuleBean.setContent(this.getEducationList());
                    break;
                case SettingModule.TYPE_PRODUCT:
                    settingModuleBean.setContent(this.getProductList());
                    break;
                case SettingModule.TYPE_CUSTOMER:
                    settingModuleBean.setContent(this.getCustomerList(item));
                    break;
                default:
                    //默认没有的类型不处理
            }
            //处理图片
            settingModuleBean.setIcon(CommUtil.getFileUrl(settingModuleBean.getIcon()));

            newList.add(settingModuleBean);
        }

        view.setData(newList);
        return view;
    }

    @RequestMapping("/guide")
    public View guide(HttpServletRequest request, HttpServletResponse response) {
        View view = new View(20000, "获取数据成功");

        List<Advert> list = this.advertService.findAll(2);
        List newList = new ArrayList();
        for (Advert item : list) {
            newList.add(item.getImage().getPath());
        }

        view.setData(newList);
        return view;
    }

    /**
     * 获取所有幻灯片操作
     *
     * @return
     */
    private List getSliceList() {
        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleSlide> settingModuleSlideList = this.settingModuleSlideService.findAll(sort);

        List newList = new ArrayList();
        for (SettingModuleSlide item : settingModuleSlideList) {
            SettingModuleSlideBean settingModuleSlideBean = new SettingModuleSlideBean();
            BeanUtils.copy(item, settingModuleSlideBean);

            settingModuleSlideBean.setImage(CommUtil.getFileUrl(settingModuleSlideBean.getImage()));

            newList.add(settingModuleSlideBean);
        }
        return newList;
    }

    /**
     * 获取所有证券开户操作
     *
     * @return
     */
    private List getSecurityOpenList() {
        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleSecurityOpen> settingModuleSecurityOpenList = this.settingModuleSecurityOpenService.findAll(sort);

        List newList = new ArrayList();
        for (SettingModuleSecurityOpen item : settingModuleSecurityOpenList) {
            SettingModuleSecurityOpenBean settingModuleSecurityOpenBean = new SettingModuleSecurityOpenBean();
            BeanUtils.copy(item, settingModuleSecurityOpenBean);

            settingModuleSecurityOpenBean.setImage(CommUtil.getFileUrl(settingModuleSecurityOpenBean.getImage()));

            newList.add(settingModuleSecurityOpenBean);
        }
        return newList;
    }

    /**
     * 获取所有公告操作
     *
     * @return
     */
    private List getNoticeList() {
        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleNotice> settingModuleNoticeList = this.settingModuleNoticeService.findAll(sort);

        List newList = new ArrayList();
        for (SettingModuleNotice item : settingModuleNoticeList) {
            SettingModuleNoticeBean settingModuleNoticeBean = new SettingModuleNoticeBean();
            BeanUtils.copy(item, settingModuleNoticeBean);

            newList.add(settingModuleNoticeBean);
        }
        return newList;
    }

    /**
     * 获取所有图标操作
     *
     * @return
     */
    private List getIconList() {
        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleIcon> settingModuleIconList = this.settingModuleIconService.findAll(sort);

        List newList = new ArrayList();
        for (SettingModuleIcon item : settingModuleIconList) {
            SettingModuleIconBean settingModuleIconBean = new SettingModuleIconBean();
            BeanUtils.copy(item, settingModuleIconBean);

            settingModuleIconBean.setImage(CommUtil.getFileUrl(settingModuleIconBean.getImage()));

            newList.add(settingModuleIconBean);
        }
        return newList;
    }

    /**
     * 获取所有教育操作
     *
     * @return
     */
    private List getEducationList() {
        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleEducation> settingModuleEducationList = this.settingModuleEducationService.findAll(sort);

        List newList = new ArrayList();
        for (SettingModuleEducation item : settingModuleEducationList) {
            SettingModuleEducationBean settingModuleEducationBean = new SettingModuleEducationBean();
            BeanUtils.copy(item, settingModuleEducationBean);

            settingModuleEducationBean.setImage(CommUtil.getFileUrl(settingModuleEducationBean.getImage()));

            //处理扩展字段
            settingModuleEducationBean.setExtra(settingModuleLogic.getTypeContent(settingModuleEducationBean.getType(), settingModuleEducationBean.getValue()));


            newList.add(settingModuleEducationBean);
        }
        return newList;
    }

    /**
     * 获取所有幻灯片操作
     *
     * @return
     */
    private List getProductList() {
        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleProduct> settingModuleProductList = this.settingModuleProductService.findAll(sort);

        List newList = new ArrayList();
        for (SettingModuleProduct item : settingModuleProductList) {
            SettingModuleProductBean settingModuleProductBean = new SettingModuleProductBean();
            BeanUtils.copy(item, settingModuleProductBean);

            settingModuleProductBean.setImage(CommUtil.getFileUrl(settingModuleProductBean.getImage()));

            newList.add(settingModuleProductBean);
        }
        return newList;
    }

    /**
     * 获取所有自定义操作
     *
     * @return
     */
    private List getCustomerList(SettingModule settingModule) {
        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleCustomer> settingModuleCustomerList = this.settingModuleCustomerService.findAll(settingModule, sort);

        List newList = new ArrayList();
        for (SettingModuleCustomer item : settingModuleCustomerList) {
            SettingModuleCustomerBean settingModuleCustomerBean = new SettingModuleCustomerBean();
            BeanUtils.copy(item, settingModuleCustomerBean);

            settingModuleCustomerBean.setImage(CommUtil.getFileUrl(settingModuleCustomerBean.getImage()));

            //处理扩展字段
            settingModuleCustomerBean.setExtra(settingModuleLogic.getTypeContent(settingModuleCustomerBean.getType(), settingModuleCustomerBean.getValue()));
            //去掉不展示的字段
            settingModuleCustomerBean.setModule(null);


            newList.add(settingModuleCustomerBean);
        }
        return newList;
    }
}